package com.tfswx.ruleengine.database.bean;

import com.tfswx.ruleengine.database.enums.SjklxEnum;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ricky
 * @date 2022/3/1
 */
public abstract class BaseTable {
    public abstract String getTableName();

    public abstract String getTableComment();

    public abstract List<TabeleField> getFieldList();

    /**
     * 建表
     *
     * @param sjklxEnum
     * @return
     */
    public List<String> getCreateSql(SjklxEnum sjklxEnum) {
        List<String> sqls = new ArrayList<>();
        switch (sjklxEnum) {
            case DM:
                // 建表语句
                sqls.add(getDmCreateSql());
                // 表注释
                sqls.add(getDmTableCommentSql());
                // 字段注释
                for (TabeleField tabeleField : getFieldList()) {
                    sqls.add(tabeleField.getDmCommentSql(getTableName()));
                }
                break;
            case MYSQL:
                // 建表语句
                sqls.add(getMysqlCreateSql());
                break;
            case SQLITE:
                sqls.add(getSqliteCreateSql());
                break;
            default:
        }
        return sqls;
    }

    /**
     * 删表
     *
     * @param sjklxEnum
     * @return
     */
    public List<String> getDropSql(SjklxEnum sjklxEnum) {
        List<String> sqls = new ArrayList<>();
        switch (sjklxEnum) {
            case DM:
                // 删表语句
                sqls.add(getDmDropSql());
                break;
            case MYSQL:
                // 删表语句
                sqls.add(getMysqlDropSql());
                break;
            case SQLITE:
                sqls.add(getSqliteDropSql());
                break;
            default:
        }
        return sqls;
    }

    /**
     * 清空表
     *
     * @param sjklxEnum
     * @return
     */
    public List<String> getTruncateSql(SjklxEnum sjklxEnum) {
        List<String> sqls = new ArrayList<>();
        switch (sjklxEnum) {
            case DM:
                // 清空表语句
                sqls.add(getDmTruncateSql());
                break;
            case MYSQL:
                // 清空表语句
                sqls.add(getMysqlTruncateSql());
                break;
            case SQLITE:
                // 清空表语句
                sqls.add(getSqliteTruncateSql());
                break;
            default:
        }
        return sqls;
    }

    /**
     * 达梦建表语句
     *
     * @return java.lang.String
     * @author Ricky
     * @date 2021/7/10
     */
    private String getDmCreateSql() {
        List<TabeleField> fieldList = getFieldList();
        StringBuilder sb = new StringBuilder();
        sb.append("CREATE TABLE ").append(getTableName()).append(" (");
        StringBuilder pk = new StringBuilder();
        fieldList.forEach(item -> {
            if (Boolean.TRUE.equals(item.getZj())) {
                pk.append(item.getZdmc()).append(",");
            }
            sb.append("\n\t").append(item.getZdlxStr(SjklxEnum.DM)).append(",");
        });
        // 主键 PRIMARY KEY('')
        pk.deleteCharAt(pk.length() - 1);
        sb.append("PRIMARY KEY(").append(pk).append(")");
        sb.append("\n").append(");");
        return sb.toString();
    }

    /**
     * 达梦表注释语句
     *
     * @return java.lang.String
     * @author Ricky
     * @date 2021/7/10
     */
    private String getDmTableCommentSql() {
        return "COMMENT ON TABLE " + getTableName() + " IS '" + getTableComment() + "';";
    }


    /**
     * Mysql建表语句
     *
     * @return java.lang.String
     * @author Ricky
     * @date 2021/7/10
     */
    private String getMysqlCreateSql() {
        List<TabeleField> fieldList = getFieldList();
        StringBuilder sb = new StringBuilder();
        sb.append("CREATE TABLE IF NOT EXISTS ").append(getTableName()).append(" (");
        fieldList.forEach(item -> sb.append("\n\t").append(item.getZdlxStr(SjklxEnum.MYSQL)).append(","));
        sb.deleteCharAt(sb.length() - 1);
        sb.append("\n").append(") COMMENT '").append(getTableComment()).append("';");
        return sb.toString();
    }

    private String getSqliteCreateSql() {
        List<TabeleField> fieldList = getFieldList();
        StringBuilder sb = new StringBuilder();
        sb.append("CREATE TABLE IF NOT EXISTS ").append(getTableName()).append(" (");
        StringBuilder pk = new StringBuilder();
        fieldList.forEach(item -> {
            if (Boolean.TRUE.equals(item.getZj())) {
                pk.append(item.getZdmc()).append(",");
            }
            sb.append("\n\t").append(item.getZdlxStr(SjklxEnum.SQLITE)).append(",");
        });
        // 主键 PRIMARY KEY('')
        pk.deleteCharAt(pk.length() - 1);
        sb.append("PRIMARY KEY(").append(pk).append(")");
        sb.append("\n").append(");");
        return sb.toString();
    }


    /**
     * 达梦删表语句
     *
     * @return java.lang.String
     * @author Ricky
     * @date 2021/7/10
     */
    private String getDmDropSql() {
        return "DROP TABLE IF EXISTS " + getTableName() + ";";
    }

    /**
     * Mysql删表语句
     *
     * @return java.lang.String
     * @author Ricky
     * @date 2021/7/10
     */
    private String getMysqlDropSql() {
        return "DROP TABLE IF EXISTS " + getTableName() + ";";
    }

    private String getSqliteDropSql() {
        return "DROP TABLE IF EXISTS " + getTableName() + ";";
    }

    /**
     * 达梦清空表语句
     *
     * @return java.lang.String
     * @author Ricky
     * @date 2021/7/10
     */
    private String getDmTruncateSql() {
        return "DELETE FROM " + getTableName() + ";";
    }

    /**
     * mysql清空表语句
     *
     * @return java.lang.String
     * @author Ricky
     * @date 2021/7/10
     */
    private String getMysqlTruncateSql() {
        return "DELETE FROM " + getTableName() + ";";
    }

    private String getSqliteTruncateSql() {
        return "DELETE FROM " + getTableName() + ";";
    }
}
